package com.pjnkj.thunder.common.utils;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

public class MapUtils {
    // private static double EARTH_RADIUS = 6378.137;
//    private static double EARTH_RADIUS = 6371.393;

//    private static double rad(double d) {
//        return d * Math.PI / 180.0;
//    }

    /**
     * 计算两个经纬度之间的距离
     *
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */

//    public static double GetDistance(String lat1, String lng1, String lat2, String lng2) {
//        double radLat1 = rad(Double.valueOf(lat1));
//        double radLat2 = rad(Double.valueOf(lat2));
//        double a = radLat1 - radLat2;
//        double b = rad(Double.valueOf(lng1)) - rad(Double.valueOf(lng2));
//        double s = 2 * Math.asin(Math.sqrt(Math.abs(
//                Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))));
//        s = s * EARTH_RADIUS;
//        s = Math.round(s * 1000);
//        return s;
//    }

    /**
     * 根据用户的起点和终点经纬度计算两点间距离，此距离为相对较短的距离，单位米。
     *
     * @param start 起点的坐标
     * @param end   终点的坐标
     * @return
     */
//    public static double GetDistance(String lat1, String lng1, String lat2, String lng2) {
//        double d1 = 0.01745329251994329D;
//        double d2 = Double.valueOf(lng1);
//        double d3 = Double.valueOf(lat1);
//        double d4 = Double.valueOf(lng2);
//        double d5 = Double.valueOf(lat2);
//        d2 *= d1;
//        d3 *= d1;
//        d4 *= d1;
//        d5 *= d1;
//        double d6 = Math.sin(d2);
//        double d7 = Math.sin(d3);
//        double d8 = Math.cos(d2);
//        double d9 = Math.cos(d3);
//        double d10 = Math.sin(d4);
//        double d11 = Math.sin(d5);
//        double d12 = Math.cos(d4);
//        double d13 = Math.cos(d5);
//        double[] arrayOfDouble2 = new double[3];
//        double[] arrayOfDouble1 = new double[3];
//        arrayOfDouble1[0] = (d9 * d8);
//        arrayOfDouble1[1] = (d9 * d6);
//        arrayOfDouble1[2] = d7;
//        arrayOfDouble2[0] = (d13 * d12);
//        arrayOfDouble2[1] = (d13 * d10);
//        arrayOfDouble2[2] = d11;
//        double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0])
//                + (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1])
//                + (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));
//
//        return (Math.asin(d14 / 2.0D) * 12742001.579854401D);
//    }
//
    private static final double EARTH_RADIUS = 6378137;//赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 基于余弦定理求两经纬度距离
     *
     * @param lon1 第一点的精度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的精度
     * @param lat2 第二点的纬度
     * @return 返回的距离，单位km
     */
    public static double GetDistance(String lon1, String lat1, String lon2, String lat2) {
        double radLat1 = rad(Double.valueOf(lat1));
        double radLat2 = rad(Double.valueOf(lat2));

        double radLon1 = rad(Double.valueOf(lon1));
        double radLon2 = rad(Double.valueOf(lon2));

        if (radLat1 < 0)
            radLat1 = Math.PI / 2 + Math.abs(radLat1);// south
        if (radLat1 > 0)
            radLat1 = Math.PI / 2 - Math.abs(radLat1);// north
        if (radLon1 < 0)
            radLon1 = Math.PI * 2 - Math.abs(radLon1);// west
        if (radLat2 < 0)
            radLat2 = Math.PI / 2 + Math.abs(radLat2);// south
        if (radLat2 > 0)
            radLat2 = Math.PI / 2 - Math.abs(radLat2);// north
        if (radLon2 < 0)
            radLon2 = Math.PI * 2 - Math.abs(radLon2);// west
        double x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1);
        double y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1);
        double z1 = EARTH_RADIUS * Math.cos(radLat1);

        double x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2);
        double y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2);
        double z2 = EARTH_RADIUS * Math.cos(radLat2);

        double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
        //余弦定理求夹角
        double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS));
        double dist = theta * EARTH_RADIUS;
        return dist;
    }

    public static double getDistanceMeter(String lat1, String lon1, String lat2, String lon2, Ellipsoid ellipsoid) {

        GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(lat1), Double.valueOf(lon1));
        GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(lat2), Double.valueOf(lon2));
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, source, target);

        return geoCurve.getEllipsoidalDistance();
    }
}
